﻿using System.Collections.Generic;
using Entitas;
using Script.Other;

public class FireSystem : ReactiveSystem<GameEntity>
{
    private readonly Contexts context;

    public FireSystem (Contexts contexts) : base (contexts.game)
    {
        context = contexts;
    }

    protected override ICollector<GameEntity> GetTrigger (IContext<GameEntity> context)
    {
        return context.CreateCollector (GameMatcher.AllOf (
            GameMatcher.PlayerTag,
            GameMatcher.ViewComp,
            GameMatcher.CreateBulletCmdComp
        ));
    }

    protected override bool Filter (GameEntity entity)
    {
        return true;
    }

    protected override void Execute (List<GameEntity> entities)
    {
        foreach (var entity in entities)
        {
            var fireCmd = entity.createBulletCmdComp;
            entity.RemoveCreateBulletCmdComp ();

            if (entity.timerComp.FireTimer > 0)
                continue;

            entity.ReplaceTimerComp (0.2f);

            var playerView = entity.viewComp.view as PlayerView;

            if (playerView == null)
                return;

            EntityUtils.CreateBulletEntity (context, playerView.shoot.position, fireCmd.angle.Angle2Vector2D (), fireCmd.angle);
        }
    }
}